/*		Filename: 	logical_server.h
 *		Author:		彭望
 *		Date:		2012-01-09
 *		Use:		定义逻辑业务服务器端的结构
 */

#ifndef LOGICAL_SERVER_H
#define LOGICAL_SERVER_H

#include <map>
#include <boost/smart_ptr.hpp>
#include <boost/any.hpp>
#include <boost/thread.hpp>

#include "public.h"
#include "logger.h"
#include "card_manager.h"
#include "accounting_manager.h"
#include "fee_manager.h"
#include "net_manager.h"
#include "session.h"

using namespace std;
using boost::any;

class CLogicalServer
{
	public:
		enum ManagerType{
				CARD_MANAGER		=		1,
				FEE_MANAGER			=		2,
				ACCOUNTING_MANAGER	=		3,
				NET_MANAGER			=		4,
				FEE_RECORD_MANAGER	=		5,
				DB_MANAGER			=		6,
		};
		typedef boost::any CManagerPtr;
		typedef CNetManager::SessionIndex_t SessionIndex_t;
		typedef CNetManager::server_callback_func_t callback_t;
		//key: ip(每台机器独立IP), value: session
		typedef map<SessionIndex_t, CSessionPtr> SessionMap;
		//key : 管理器类型, value:各个服务管理器
		typedef map<ManagerType, CManagerPtr> ManagerMap;
		

	public:
		CLogicalServer(const string& strConfigFilename);
		CManagerPtr GetManager(ManagerType key);
		//监听客户端传来的消息
		RetCode_t listen();

		static CSessionPtr make_session(const CLogicalServer& );


	private:

		//载入配置文件
		RetCode_t loadConfig(const string& strContfigFilename);
		//初始化程序各个组件(监听端口等)
		RetCode_t init();

		void listen_callback(const SessionIndex_t& index, istream& is, ostream& os);
	private:
		SessionMap m_sessions;	
		ManagerMap m_managers;

		short m_listenPort;
		short m_clientPort;
		Logger::Logger_t& logger;
		string m_dbConfigName;
};

#endif
